package com.sailer.practice.guava.thread;

import com.sailer.practice.guava.thread.config.ThrearPoolConfig;
import com.sailer.practice.model.WorkParam;
import com.sailer.practice.model.Worker;
import org.apache.commons.lang3.time.StopWatch;

import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;

public class CompletionServiceTest {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        int taskCount=200;
        CompletionService<String> cs = new ExecutorCompletionService(new ThrearPoolConfig().jobExecutor());
        for (int i=0;i<taskCount;i++){
            cs.submit(new Worker(new WorkParam(Double.parseDouble(String.valueOf(i)),"这是第"+i+"个任务")));
        }
        System.out.println("@@@@任务提交完成");
        StopWatch watch=new StopWatch();
        watch.start();
        for (int i=0;i<taskCount;i++){
            String result= cs.take().get();
            System.out.println(result);
        }
        watch.stop();
        System.out.println("@@@@   done :"+watch.getNanoTime()/1000000L);
        System.exit(0);
    }


}
